package pig.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import pig.model.AdminDAO;
import pig.model.AdminVO;
import pig.model.CustomerVO;
import pig.model.DealVO;
import pig.model.SellerDAO;
import pig.model.SellerVO;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Resource(name="adminDAOImpl")
	private AdminDAO adminDAO;
	
	@Resource(name="sellerDAOImpl")
	private SellerDAO sellerDAO;

	/**
	 * 관리자 로그인 화면으로 이동
	 */
	@RequestMapping("admin_Login_view.do")
	public String admin_Login(){
		return "admin_Login_view.admin";
	}
	
    /**
     * 관리자 로그인
     */
    @RequestMapping("admin_login.do") 
   public String adminLogin(HttpServletRequest request,HttpServletResponse response,String id,String password){
    	String path=null;
    	AdminVO adminVO=adminDAO.adminLogin(id, password);
    	if(adminVO!=null){
    		HttpSession session=request.getSession();
    		session.setAttribute("login",adminVO);
    		path="index";
    	}else{
    		path="admin_fail.admin";
    	}
    	return path;
	}
    
    /**
     * 구매자 리스트
     */
    @RequestMapping("auth_findcustomerList.do")
    public ModelAndView customerList(String check){
    	List<CustomerVO> list =(List<CustomerVO>)adminDAO.findcustomerList();
    	return new ModelAndView("customer_List.admin","list",list);
    }
    
    /**
     * 판매자 리스트
     */
    @RequestMapping("auth_findsellerList.do")
    public ModelAndView sellerList(String check){
    	List<SellerVO> list = (List<SellerVO>)adminDAO.findsellerList();
    	return new ModelAndView("seller_List.admin","list",list);
    }
    
    /**
     * 판매자 가입승인
     */
    @RequestMapping("notauthSeller.do")
    @ResponseBody
    public SellerVO notauthSeller(String sellerId){
    	SellerVO sellerVO = adminDAO.getSellerVOById(sellerId);
		if(sellerVO.getPermission().equals("0")){ //퍼미션이 1이면 가입승인이 되지 않은 상태
			adminDAO.updatePermission(sellerVO.getSellerId());
			sellerVO = adminDAO.getSellerVOById(sellerId);
		}
		return sellerVO;
    }
    
    /**
     * 상품 삭제
     */
    @RequestMapping("/deleteDeal.do")
	public ModelAndView deleteDeal(DealVO dealVO){
		adminDAO.deleteDeal(dealVO);
		return new ModelAndView("index");
	}
}
